Homework Assignment #1

Your first homework assignment for this week is to take the variable named "text" (this has been defined for you) and count the number of times "z" occurs AND the letter "k" occurs. Add those numbers up and print the result.

As a further complication, we DO NOT care about case (e.g. ‘z’ and ‘Z’ should both be included in the count).

Don’t feel bad if you struggle, this homework is a step up in difficulty compared to normal. Oh and I have also included a few test cases below that should help you figure out what do to (just in case my instructions were not clear enough).

For bonus difficulty, make your code work for any letter (e.g. "a", "b" returns the count of 'a' + 'A' + 'b' + 'B').


In [ ]:
text = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzZZZZeeeewwwwwwwwkKewewe2324____23!!!!!fsdffskdsdzzzzZZZZZZZZZZZZZZZZroiooioi"

# Simple Examples (string --> total you should return)
# "zZ"       --> 2
# "kK"       --> 2
# "KZ"       --> 2
# "1aZZZabc" --> 3
# "hello"    --> 0
# "ZzKk"     --> 4


# Your code goes here...

Possible Solutions...


In [4]:
text = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzZZZZeeeewwwwwwwwkKewewe2324____23!!!!!fsdffskdsdzzzzZZZZZZZZZZZZZZZZroiooioi"

##################################### Solution #1  ###################################

a = "Z"  # set a,b to any letter you like in order to solve the bonus problem.
b = "K"
text2 = text.upper()
result = text2.count(a) + text2.count(b)
print("Solution 1: ", result)

##################################### Solution #2  ###################################

a1 = "Z"
a2 = "z"
b1 = "K"
b2 = "k"
result2 = text.count(a1) + text.count(a2) + text.count(b1) + text.count(b2)
print ("Solution 2: ", result2)

##################################### Solution #3  ###################################

# Note, we shall cover for-loops later on in this course.

seq = "KZkz"  # <--- change this to solve bonus problem
counter = 0
for character in text:
    if character in seq:
        counter += 1
print("Solution 3: ", counter)

##################################### Solution #4  ###################################

# Note, this is an advanced solution, I don't expect you to understand it.
# I just wanted to show off, lol

seq = "KZkz"
result4 = len([i for i in text if i in seq])
print("Solution 4: ", result4)


Solution 1:  57
Solution 2:  57
Solution 3:  57
Solution 4:  57

Okay so what’s the best solution?

Well solution #1 is more concise the solution #2, instead of looking for “kK” the code just converts everything to upper-case which means it only has to count capital letters. In order to save two or three lines of code we create a new string (or overwrite the old one) and call upper on it. For very very large strings this is probably quite slower (but still probably faster than solution #2).

Solution #4 is basically looking at every character in the string and if it is a character it is looking for (e.g “K” or “z”) it adds it the list and returns the length of the list. In terms of performance this solution is building a list (which could cost a lot of memory) only to discard it and return a single number (i.e. the length of the list). On the bright-side, at just two lines of code its the most concise solution we have.

Overall though, I think solution #3 is probably the best; it should be decent in terms of performance (both memory and speed) and is quite readable.